package com.spring.Controller;

import com.spring.Controller.Interface.PaylogController;
import com.spring.Entity.PaylogEntity;
import com.spring.Service.Interface.PayLogService;
import com.spring.Util.STATUS;
import jxl.read.biff.BiffException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Arabira on 2016/8/4.
 */
@Controller
@RequestMapping("/payLog")
public class PaylogControllerImpl implements PaylogController {
    @Autowired
    private PayLogService payLogService;

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Map<String, Object> addPaylog(PaylogEntity paylogEntity, HttpSession session, HttpServletRequest request) {
        Map<String, Object> response = new HashMap<String, Object>();
        String userId = (String) session.getAttribute("SID");
        if (StringUtils.isBlank(paylogEntity.getContractId()) || StringUtils.isBlank(userId)) {
            response.put("status", "empty");
            response.put("description", "合同号为空或未登录");
            return response;
        }
        int status = payLogService.addPaylog(paylogEntity);
        if (status == STATUS.SUCCESS) {
            response.put("status", "ok");
            response.put("description", "添加成功");
        }
        if (status == STATUS.MONEYERROR) {
            response.put("status", "error");
            response.put("description", "金额不可为负数");
        }
        return response;
    }

    //通过合同ID查支付记录
    @ResponseBody
    @RequestMapping(value = "/byId", method = RequestMethod.POST)
    public Map<String, Object> findPaylogByContractId(String contractId, HttpSession session, HttpServletRequest request) {
        Map<String, Object> response = new HashMap<String, Object>();
        if (session.getAttribute("SID").equals("null")) {
            response.put("status", "refuse");
            response.put("decription", "未登录");
            return response;
        }
        if (StringUtils.isBlank(contractId)) {
            response.put("status", "error");
            response.put("description", "合同ID值为空值");
            return response;
        } else {
            response = payLogService.findByContractId(contractId);
        }
        return response;
    }

    @ResponseBody
    @RequestMapping(value = "/upOrDown", method = RequestMethod.POST)
    public Map<String, Object> upOrDown(Integer paylogId, HttpSession session, HttpServletRequest request) {
        Map<String, Object> response = payLogService.onOrOff(paylogId);
        return response;
    }

    @Override
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Map<String, Object> updatePaidTime(PaylogEntity paylogEntity, HttpSession session, HttpServletRequest request) {
        Map<String, Object> response = new HashMap<>();
        if (session.getAttribute("SID").equals("null")) {
            response.put("status", "refuse");
            response.put("decription", "未登录");
            return response;
        }
        String test = paylogEntity.getContractId();
        int test2 = paylogEntity.getId();

        if (StringUtils.isBlank(paylogEntity.getContractId()) || paylogEntity.getId() == 0) {
            response.put("status", "empty");
            response.put("description", "合同id或支付id为空");
            return response;
        }
        int status = payLogService.update(paylogEntity.getId(), paylogEntity.getPaidTime(), paylogEntity.getRemindTime(), paylogEntity.getMoney());
        if (status == STATUS.SUCCESS) {
            response.put("status", "ok");
            response.put("description", "修改成功");
        }
        if (status == STATUS.PAIDTIMEERROR) {
            response.put("status", "error");
            response.put("description", "支付时间不可为已记录的的时间之前的时间");
        }
        if (status == STATUS.REMINDTIMEERROR) {
            response.put("status", "error");
            response.put("description", "提醒时间不可为已记录的当前时间之前的时间");
        }
        if (status == STATUS.MONEYERROR) {
            response.put("status", "error");
            response.put("description", "金额不可为负数");
        }
        return response;
    }

    @ResponseBody
    @RequestMapping(value = "/addExcel", method = RequestMethod.POST)
    public Map<String, Object> addPaylogExcel(MultipartFile file, HttpServletRequest request) throws IOException, BiffException, ParseException {
        Map<String, Object> response = new HashMap<String, Object>();
        if (file == null) {
            response.put("status", "empty");
            response.put("description", "提交文件为空");
            return response;
        }
        String message = payLogService.addByExcel(file);
        if (StringUtils.isBlank(message)) {
            response.put("status", "ok");
            response.put("description", "添加成功");
        } else {
            response.put("status", "error");
            response.put("description", message);
        }
        return response;
    }
}
